// BO PHAN KHO:PHAN LOAI HANG, GIAO HANG
package com.sun.courier.view;

import com.sun.courier.view.support.ExportingStore_ButtonsRenderer;
import com.sun.courier.view.support.ExportingStore_ButtonsEditor;
import com.sun.courier.view.support.ImportedStore_ButtonsRenderer;
import com.sun.courier.view.support.ImportedStore_ButtonsEditor;
import com.sun.courier.DemoProperties;
import com.sun.courier.dal.CourierDAL;
import com.sun.courier.dal.CourierManageDAL;
import com.sun.courier.dto.Courier_CourierManageDTO;
import com.sun.courier.utilities.Utilities;
import com.sun.courier.view.support.Exported_ButtonsEditor;
import com.sun.courier.view.support.Exported_ButtonsRenderer;
import com.sun.courier.view.support.StoreConfirm_ButtonsEditor;
import com.sun.courier.view.support.StoreConfirm_ButtonsRenderer;
import com.sun.courier.view.support.StoreExport_ButtonsEditor;
import com.sun.courier.view.support.StoreExport_ButtonsRenderer;
import com.sun.courier.view.support.StoreReturnCus_ButtonsEditor;
import com.sun.courier.view.support.StoreReturnCus_ButtonsRenderer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author HONG QUAN
 */
@DemoProperties(
        value = "Storing",
        category = "Courier Management",
        description = "Store And Clasify Couriers",
        sourceFiles = { //                "com/sun/courier/view/EmployeeManagement.java",
        //                "com/sun/courier/view/resources/images/EmployeeManagement.png",
        }
)
public class CourierStoring extends JPanel implements ActionListener {

    JTable confirmTable = null;
    JScrollPane scrollpane = null;
    DefaultTableModel model = null;
    JPanel controlPanel;
    JComboBox cbSelectType = new JComboBox();

    JLabel search = new JLabel("Search:");
    JTextField txtSearch = new JTextField();

    CourierManageDAL cmDal = new CourierManageDAL();
    CourierDAL cDal = new CourierDAL();

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    private TableRowSorter<TableModel> rowSorter;

    String empId = Utilities.empId;
    String username = Utilities.username;

    public CourierStoring() {
        ActionListeners();
        setLayout(new BorderLayout());
        controlPanel = createControlPanel();
        add(controlPanel, BorderLayout.NORTH);
        createCourierWillinStoreJTable();
    }

    private JPanel createControlPanel() {
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(gridbag);

        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        c.insets = new Insets(20, 10, 0, 10);
        c.anchor = GridBagConstraints.SOUTHWEST;
        controlPanel.add(search, c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 1;
        c.insets.top = 0;
        c.insets.bottom = 0;
        c.insets.left = 5;
        c.anchor = GridBagConstraints.SOUTHWEST;
        txtSearch.setPreferredSize(new Dimension(300, 30));
        controlPanel.add(txtSearch, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridheight = 1;
        c.insets = new Insets(0, 10, 0, 10);
        c.anchor = GridBagConstraints.SOUTHWEST;
        JLabel courierSelection = new JLabel("Select A Type of Courier Management Method:");
        controlPanel.add(courierSelection, c);

        c.gridx = 0;
        c.gridy = 3;
        c.weightx = 1.0;
        c.insets.top = 0;
        c.insets.bottom = 12;
        c.anchor = GridBagConstraints.SOUTHWEST;
        cbSelectType.setPreferredSize(new Dimension(300, 30));
//        cbSelectType.addItem("Choose An Item");
        cbSelectType.addItem("Couriers Will Import Store");//= Hang da nhan tu dong nghiep (Dung de tra KH)
        cbSelectType.addItem("Couriers Export To Despatching");//Lam roi voi status = 6
        cbSelectType.addItem("Couriers Imported Store");
        cbSelectType.addItem("Couriers Exported");
        cbSelectType.addItem("Couriers Export To Return Customer");// XONG
        cbSelectType.addItem("Couriers Exporting");// CAI NAY CHUA TAO
        controlPanel.add(cbSelectType, c);

        return controlPanel;
    }

    class MyItemListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            Object source = e.getSource();
            if (source instanceof AbstractButton == false) {
                return;
            }
            boolean checked = e.getStateChange() == ItemEvent.SELECTED;
            for (int x = 0, y = confirmTable.getRowCount(); x < y; x++) {
                confirmTable.setValueAt(new Boolean(checked), x, 7);
            }
        }
    }
////////////////////////////////////////////////0. Couriers Will Import Store/////////// Hang dang duoc gui toi kho

    private void createCourierWillinStoreJTable() {
        confirmTable = CourierWillinStoreJTable();
//        table.setColumnModel(createColumnModel());
//        confirmTable.setAutoCreateRowSorter(true);
        rowSorter = new TableRowSorter<>(confirmTable.getModel());
        confirmTable.setRowSorter(rowSorter);
        confirmTable.setRowHeight(30);
        confirmTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        confirmTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        confirmTable.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = confirmTable.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * confirmTable.getRowHeight();
        confirmTable.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = confirmTable.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(confirmTable);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable CourierWillinStoreJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Sender", "Status", "Update Time", "Actions", "Sender"};
        Object[][] data = null;
        ArrayList<Courier_CourierManageDTO> lst = null;
        try {
            lst = cmDal.loadCourierWilImportStore(Integer.parseInt(empId));//Login Id 4 hoac 5
            data = new Object[lst.size()][9];
            for (int i = 0; i < lst.size(); i++) {
                data[i][0] = lst.get(i).getC().getCourierId();
                data[i][1] = lst.get(i).getC().getCourierName();
                data[i][2] = lst.get(i).getC().getCustomerName();
                data[i][3] = lst.get(i).getC().getPhone();
                data[i][4] = cDal.getEmployeeName(lst.get(i).getCm().getSender());
                data[i][5] = "Confirm Requirement";
                data[i][6] = lst.get(i).getCm().getUpdatetime();
                data[i][7] = empty;
                data[i][8] = lst.get(i).getCm().getSender();
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (lst.isEmpty()) {
            model = new DefaultTableModel(data, columnNames);
        } else {
            model = new DefaultTableModel(data, columnNames) {
                @Override
                public Class<?> getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
            };
        }

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(7);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);
        TableColumn column8 = table.getColumnModel().getColumn(8);
        column8.setMaxWidth(0);
        column8.setMinWidth(0);
        column8.setWidth(0);
        column8.setPreferredWidth(0);

        columnActions.setCellRenderer(new StoreConfirm_ButtonsRenderer());
        columnActions.setCellEditor(new StoreConfirm_ButtonsEditor(table));
        return table;
    }

    // HANG TRONG KHO, SE XUAT HANG CHO NGUOI VAN CHUYEN: 2. Courier To Export To Despatching
    private void createCourierWillExportJTable() {
        confirmTable = CourierWillExportJTable();
//        table.setColumnModel(createColumnModel());
//        confirmTable.setAutoCreateRowSorter(true);
        rowSorter = new TableRowSorter<>(confirmTable.getModel());
        confirmTable.setRowSorter(rowSorter);
        confirmTable.setRowHeight(30);
        confirmTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        confirmTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        confirmTable.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = confirmTable.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * confirmTable.getRowHeight();
        confirmTable.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = confirmTable.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(confirmTable);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable CourierWillExportJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Receiver", "Status", "Update Time", "Actions"};
        Object[][] data = null;
        try {
            ArrayList<Courier_CourierManageDTO> lst = cmDal.loadCourierInStore(Integer.parseInt(empId));//Login Id
            data = new Object[lst.size()][8];
            for (int i = 0; i < lst.size(); i++) {
                data[i][0] = lst.get(i).getC().getCourierId();
                data[i][1] = lst.get(i).getC().getCourierName();
                data[i][2] = lst.get(i).getC().getCustomerName();
                data[i][3] = lst.get(i).getC().getPhone();
                data[i][4] = cDal.getEmployeeName(lst.get(i).getCm().getReceiver());
                int status = lst.get(i).getCm().getStatus();
                if (status == 6) {
                    data[i][5] = "In Receiving Store";
                } else if (status == 8) {
                    data[i][5] = "In Return Store";
                }
                data[i][6] = lst.get(i).getCm().getUpdatetime();
                data[i][7] = empty;
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        model = new DefaultTableModel(data, columnNames) {
            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(7);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);

        columnActions.setCellRenderer(new StoreExport_ButtonsRenderer());
        columnActions.setCellEditor(new StoreExport_ButtonsEditor(table));
        return table;
    }

    /////////////////////////////////////Courier Imported JTable//////////////////////////
    private void createCourierImportedJTable() {
        confirmTable = CourierImportedJTable();
//        table.setColumnModel(createColumnModel());
        rowSorter = new TableRowSorter<>(confirmTable.getModel());
        confirmTable.setRowSorter(rowSorter);
        confirmTable.setRowHeight(30);
        confirmTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        confirmTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        confirmTable.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = confirmTable.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * confirmTable.getRowHeight();
        confirmTable.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = confirmTable.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(confirmTable);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable CourierImportedJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Sender", "Status", "Update Time", "Actions", "SenderId"};
        Object[][] data = null;
        try {
            ArrayList<Courier_CourierManageDTO> lst = cmDal.loadCourierImportedStore(Integer.parseInt(empId));//Login Id
            data = new Object[lst.size()][9];
            for (int i = 0; i < lst.size(); i++) {
                data[i][0] = lst.get(i).getC().getCourierId();
                data[i][1] = lst.get(i).getC().getCourierName();
                data[i][2] = lst.get(i).getC().getCustomerName();
                data[i][3] = lst.get(i).getC().getPhone();
                data[i][4] = cDal.getEmployeeName(lst.get(i).getCm().getSender());
                data[i][5] = "Imported";
                data[i][6] = lst.get(i).getCm().getUpdatetime();
                data[i][7] = empty;
                data[i][8] = lst.get(i).getCm().getSender();
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        model = new DefaultTableModel(data, columnNames) {
            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(7);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);
        TableColumn column8 = table.getColumnModel().getColumn(8);
        column8.setMaxWidth(0);
        column8.setMinWidth(0);
        column8.setWidth(0);
        column8.setPreferredWidth(0);

        columnActions.setCellRenderer(new ImportedStore_ButtonsRenderer());
        columnActions.setCellEditor(new ImportedStore_ButtonsEditor(table));
        return table;
    }

    //////////////////////////////////////COURIER EXPORTED STORE////////////////
    private void createCourierExportedJTable() {
        confirmTable = CourierExportedJTable();
//        table.setColumnModel(createColumnModel());
        rowSorter = new TableRowSorter<>(confirmTable.getModel());
        confirmTable.setRowSorter(rowSorter);
        confirmTable.setRowHeight(30);
        confirmTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        confirmTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        confirmTable.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = confirmTable.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * confirmTable.getRowHeight();
        confirmTable.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = confirmTable.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(confirmTable);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable CourierExportedJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Receiver", "Status", "Update Time", "Actions", "ReceiverId"};
        Object[][] data = null;
        try {
            ArrayList<Courier_CourierManageDTO> lst = cmDal.loadCourierExportedStore(Integer.parseInt(empId));//Login Id
            data = new Object[lst.size()][9];
            for (int i = 0; i < lst.size(); i++) {
                data[i][0] = lst.get(i).getC().getCourierId();
                data[i][1] = lst.get(i).getC().getCourierName();
                data[i][2] = lst.get(i).getC().getCustomerName();
                data[i][3] = lst.get(i).getC().getPhone();
                data[i][4] = cDal.getEmployeeName(lst.get(i).getCm().getReceiver());
                data[i][5] = "Exported";
                data[i][6] = lst.get(i).getCm().getUpdatetime();
                data[i][7] = empty;
                data[i][8] = lst.get(i).getCm().getReceiver();
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        model = new DefaultTableModel(data, columnNames) {
            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(7);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);
        TableColumn column8 = table.getColumnModel().getColumn(8);
        column8.setMaxWidth(0);
        column8.setMinWidth(0);
        column8.setWidth(0);
        column8.setPreferredWidth(0);

        columnActions.setCellRenderer(new Exported_ButtonsRenderer());
        columnActions.setCellEditor(new Exported_ButtonsEditor(table));
        return table;
    }

    ///////////////////////////////////////////////////////COURIER EXPORT TO RETURN CUSTOMER/////////////////////////////
    private void createStoreReturnCustomerJTable() {
        confirmTable = CourierStoreReturnCustomerJTable();
//        table.setColumnModel(createColumnModel());
        rowSorter = new TableRowSorter<>(confirmTable.getModel());
        confirmTable.setRowSorter(rowSorter);
        confirmTable.setRowHeight(30);
        confirmTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        confirmTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        confirmTable.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = confirmTable.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * confirmTable.getRowHeight();
        confirmTable.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = confirmTable.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(confirmTable);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable CourierStoreReturnCustomerJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Receiver", "Status", "Update Time", "Actions"};
        Object[][] data = null;
        ArrayList<Courier_CourierManageDTO> lst = null;
        try {
            lst = cmDal.loadExportReturnCustomerStore(Integer.parseInt(empId));//Login Id
            if (lst.isEmpty()) {
                data = new Object[0][9];
            } else {
                data = new Object[lst.size()][9];
                for (int i = 0; i < lst.size(); i++) {
                    data[i][0] = lst.get(i).getC().getCourierId();
                    data[i][1] = lst.get(i).getC().getCourierName();
                    data[i][2] = lst.get(i).getC().getCustomerName();
                    data[i][3] = lst.get(i).getC().getPhone();
                    data[i][4] = cDal.getEmployeeName(lst.get(i).getCm().getSender());
                    data[i][5] = "In Store";
                    data[i][6] = lst.get(i).getCm().getUpdatetime();
                    data[i][7] = empty;
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (lst.isEmpty()) {
            model = new DefaultTableModel(data, columnNames);
        } else {
            model = new DefaultTableModel(data, columnNames) {
                @Override
                public Class<?> getColumnClass(int column) {
                    return getValueAt(0, column).getClass();
                }
            };
        }

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(7);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);

        columnActions.setCellRenderer(new StoreReturnCus_ButtonsRenderer());
        columnActions.setCellEditor(new StoreReturnCus_ButtonsEditor(table));
        return table;
    }

    ///////////////////////////////////////////////////////////////COURIER EXPORTING////////////////////////////////////////////
    private void createCourierStoreExportingJTable() {
        confirmTable = CourierStoreExportingJTable();
//        table.setColumnModel(createColumnModel());
        rowSorter = new TableRowSorter<>(confirmTable.getModel());
        confirmTable.setRowSorter(rowSorter);
        confirmTable.setRowHeight(30);
        confirmTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
        confirmTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        confirmTable.setIntercellSpacing(new Dimension(0, 0));

        Dimension viewSize = new Dimension();
        viewSize.width = confirmTable.getColumnModel().getTotalColumnWidth();
        viewSize.height = 10 * confirmTable.getRowHeight();
        confirmTable.setPreferredScrollableViewportSize(viewSize);
//        </snip> Right click on JTable

        //<snip>
        JTableHeader header = confirmTable.getTableHeader();
        header.setPreferredSize(new Dimension(30, 26));
        TableCellRenderer headerRenderer = header.getDefaultRenderer();
        if (headerRenderer instanceof JLabel) {
            ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
        }

        scrollpane = new JScrollPane(confirmTable);
        add(scrollpane, BorderLayout.CENTER);
    }

    public JTable CourierStoreExportingJTable() {
        String empty = "";
        String[] columnNames = {"Courier Id", "Courier Name", "Customer Name", "Phone",
            "Receiver", "Status", "Update Time", "Actions", "ReceiverId"};
        Object[][] data = null;
        try {
            ArrayList<Courier_CourierManageDTO> lst = cmDal.loadCourierStoreExporting(Integer.parseInt(empId));//Login Id
            data = new Object[lst.size()][9];
            for (int i = 0; i < lst.size(); i++) {
                data[i][0] = lst.get(i).getC().getCourierId();
                data[i][1] = lst.get(i).getC().getCourierName();
                data[i][2] = lst.get(i).getC().getCustomerName();
                data[i][3] = lst.get(i).getC().getPhone();
                data[i][4] = cDal.getEmployeeName(lst.get(i).getCm().getReceiver());
                data[i][5] = "Exporting";
                data[i][6] = lst.get(i).getCm().getUpdatetime();
                data[i][7] = empty;
                data[i][8] = lst.get(i).getCm().getReceiver();
            }
        } catch (Exception ex) {
            Logger.getLogger(CourierManagement.class.getName()).log(Level.SEVERE, null, ex);
        }

        model = new DefaultTableModel(data, columnNames) {
            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };

        final JTable table = new JTable(model);
//        table.setRowHeight(26);
        table.setAutoCreateRowSorter(true);
        //table.addMouseListener(new CellButtonsMouseListener());
        //ButtonsEditorRenderer er = new ButtonsEditorRenderer(table);
        TableColumn columnActions = table.getColumnModel().getColumn(7);//CHU Y CHO NAY DE TAO JBUTTON
        TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setMaxWidth(0);
        column0.setMinWidth(0);
        column0.setWidth(0);
        column0.setPreferredWidth(0);
        TableColumn column8 = table.getColumnModel().getColumn(8);
        column8.setMaxWidth(0);
        column8.setMinWidth(0);
        column8.setWidth(0);
        column8.setPreferredWidth(0);

        columnActions.setCellRenderer(new ExportingStore_ButtonsRenderer());
        columnActions.setCellEditor(new ExportingStore_ButtonsEditor(table));
        return table;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public void ActionListeners() {
        cbSelectType.addActionListener(this);
        searchFilter();
    }

    public void searchFilter() {
        txtSearch.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = txtSearch.getText();

                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = txtSearch.getText();

                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(cbSelectType)) {
            if (cbSelectType.getSelectedIndex() == 0) {
                if (scrollpane == null) {
                    createCourierWillinStoreJTable();//OK
                } else {
                    remove(scrollpane);
                    createCourierWillinStoreJTable();
                    invalidate();
                    validate();
                    repaint();
                }
            } else if (cbSelectType.getSelectedIndex() == 1) {
                remove(scrollpane);
                createCourierWillExportJTable();//OK
                invalidate();
                validate();
                repaint();
            } else if (cbSelectType.getSelectedIndex() == 2) {
                remove(scrollpane);
                createCourierImportedJTable();//OK
                invalidate();
                validate();
                repaint();
            } else if (cbSelectType.getSelectedIndex() == 3) {
                remove(scrollpane);
                createCourierExportedJTable();
                invalidate();
                validate();
                repaint();
            } else if (cbSelectType.getSelectedIndex() == 4) {
                remove(scrollpane);
                createStoreReturnCustomerJTable();
                invalidate();
                validate();
                repaint();
            } else if (cbSelectType.getSelectedIndex() == 5) {
                remove(scrollpane);
                createCourierStoreExportingJTable();
                invalidate();
                validate();
                repaint();
            }
        }
    }
}
